package com.syun.quartz.example;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzSchedule {

    @Bean
     public void task() throws Exception{
        /**
         *  1、创建调度器
         *  2、创建任务对象，绑定任务类并为其命名及分组
         *  3、创建触发器，简单触发器每隔 5 秒执行一次任务
         *  4、关联任务和触发器
         *  5、启动调度器
         */
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                .withIdentity("simpleJob","myJobGroup")
                .usingJobData("job_param","job_param")
                .build();

        JobDetail jobDetail2 = JobBuilder.newJob(MyJob.class)
                .withIdentity("cronJob","myJobGroup")
                .usingJobData("job_param","job_param")
                .build();

        //简单触发器
        Trigger simpleTrigger = TriggerBuilder.newTrigger()
                .withIdentity("simpleTrigger","simpleTriggerGroup")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(5).repeatForever())
                .build();
        //cron触发器
        Trigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity("cronTrigger","cronTriggerGroup")
                .startNow()
                .withSchedule(CronScheduleBuilder.cronSchedule("*/3 * * * * ?"))
                .build();

        scheduler.scheduleJob(jobDetail,simpleTrigger);

        scheduler.scheduleJob(jobDetail2,cronTrigger);

        scheduler.start();
     }
}
